package cn.itcast.dao;

import cn.itcast.domain.User;
import org.springframework.jdbc.core.RowMapper;
import org.springframework.jdbc.core.support.JdbcDaoSupport;

import java.sql.ResultSet;
import java.sql.SQLException;

/**
 * @author: cuisensen
 * data: 2018/6/18.
 * time: 17:07.
 */
public class UserDaoImp extends JdbcDaoSupport implements UserDao {
    @Override
    public void increaseMoney(Integer id, Double money) {
        super.getJdbcTemplate().update("UPDATE acu SET money=money+? WHERE id=?",money,id);
        User user = super.getJdbcTemplate().queryForObject("select * from acu where id=?", new RowMapper<User>() {

            @Override
            public User mapRow(ResultSet resultSet, int i) throws SQLException {
                User u=new User();
                u.setId(resultSet.getInt("id"));
                u.setName(resultSet.getString("name"));
                u.setMoney(resultSet.getDouble("money"));
                return u;
            }
        },id);
        System.out.println(user);
    }

    @Override
    public void decreaseMoney(Integer id, Double money) {
        super.getJdbcTemplate().update("UPDATE acu SET money=money-? WHERE id=?",money,id);
        User user = super.getJdbcTemplate().queryForObject("select * from acu where id=?", new RowMapper<User>() {

            @Override
            public User mapRow(ResultSet resultSet, int i) throws SQLException {
                User u=new User();
                u.setId(resultSet.getInt("id"));
                u.setName(resultSet.getString("name"));
                u.setMoney(resultSet.getDouble("money"));
                return u;
            }
        },id);
        System.out.println(user);
    }
}
